﻿
$(window).load(function () {
    $("#J_NewAddressBtn").click(function () {
        ShowAddress("");
    });
    updateorderaddr();
    $("#J_Go").click(function () {
        doSaveOrder();
    });
    $("#address-list li").live("click", function () {
        var $self = $(this);
        if ($self.find(" input").attr('disabled') == "disabled" || $self.find(" input").attr('disabled') == true) {
            return;
        }
        $self.prevAll().removeClass("selected");
        $self.nextAll().removeClass("selected");
        $self.addClass("selected").find(" input").attr("checked", true);
        updateorderaddr();
    });
    $(".J_Modify").live("click", function () {
        ShowAddress($("#address-list").find("input:radio[name='address']:checked").val());
    });
    $("#btnConfirmAddr").click(function () {
        doloadGoodsList();
    });
    $("#btnReSelAddr").click(function () {
        $("#address-list input").attr("disabled", false);
        $("#J_NewAddressBtn").removeClass("disabled");
        $('#J_Go').removeClass('btn-go-disabled').addClass('btn-go-disabled');
        $("#btnConfirmAddr").show();
        $("#btnReSelAddr").hide();
    });
    $("#btnReSelAddr").hide();

    $("#tmallPoint_1").click(function () {
        if ($(this).attr("checked") == true || $(this).attr("checked") == "checked") {
            $("#score_input").show();
            $("#score_input").prev().show();
        } else {
            $("#score_input").hide();
            $("#score_input").prev().hide();
            $('#txtPaidScoreQty').val('0');
            StaticPayAmt();
        }
    });
    $("#txtPaidScoreQty").keyup(function () {
        if (!$(this).val()) { $(this).val('0'); }
        if (!$(this).val().match(/^\d+$/)) { $(this).val('0'); }
        if ($(this).val()) {
            if (parseInt($(this).val()) > parseInt($("#spnAllowUseScore").html())) {
                $(this).val($("#spnAllowUseScore").html());
            }
            if (parseInt($(this).val()) > parseInt($('#txtPaidScoreQty').attr('max-score'))) {
                $(this).val($('#txtPaidScoreQty').attr('max-score'));
            }
            StaticPayAmt();
        }
    });
});

function StaticPayAmt() {
    var iscoreamt = parseInt($('#txtPaidScoreQty').val()) * parseFloat($('#txtPaidScoreQty').attr('score-price'));
    $("#scoredikouAmt").html(iscoreamt.toFixed(2));
    $("#tatoldikouAmt").html(parseFloat($("#scoredikouAmt").html()).toFixed(2));
    $("#J_ActualFee").html((parseFloat($("#spnPAYABLE_AMT").html()) - parseFloat($("#tatoldikouAmt").html())).toFixed(2));
};

function updateTableHtml(_data) {
    jQuery.loadHtml('/Template/ConfirmOrderList.html').done(function (_rethtml) {
        var tpl = HtmlUtil.htmlDecode(_rethtml);
        laytpl(tpl).render(_data, function (render) {
            $("#J_OrderList").empty().html(render);
        });
    });
};
function doloadGoodsList() {
    var $addrtem1 = $("#address-list").find("input:radio[name='address']:checked");
    if ($addrtem1.length <= 0) {
        alert("请选择收货地址！");
        return;
    }
    $("#address-list input").attr("disabled", true);
    $("#J_NewAddressBtn").removeClass("disabled").addClass("disabled");
    $("#btnConfirmAddr").hide();
    $("#btnReSelAddr").show();

    $("#txtPlaceId").val($addrtem1.attr("place-id"));
    var _jsParam = new jsonRow();
    _jsParam.AddCell("ACTION", "doloadGoodsList");
    _jsParam.AddCell("txtGoodsId", $("#txtGoodsId").val());
    _jsParam.AddCell("txtCartId", $("#txtCartId").val());
    _jsParam.AddCell("txtSrcT", $("#txtSrcT").val());
    _jsParam.AddCell("txtPubNId1", $("#txtPubNId1").val());
    _jsParam.AddCell("txtPubNId2", $("#txtPubNId2").val());
    _jsParam.AddCell("txtBuyQty", $("#txtBuyQty").val());
    _jsParam.AddCell("txtPlaceId", $("#txtPlaceId").val());
    _jsParam.AddCell("RECE_ADDRESS_ID", $addrtem1.val());
    ajaxComm(_jsParam);
};
function setLogisAmt(sDealId, dLogisamt, dDealAmt) {
    var $orderlisttem1 = $("#dByOrderList .order[shop-id='" + sDealId + "']");
    if ($orderlisttem1.length > 0) {
        $orderlisttem1.find(".order-logistic-amt").html(dLogisamt);
        $orderlisttem1.find(".J_ShopTotal").html((parseFloat(dDealAmt) + parseFloat(dLogisamt)).toFixed(2));
    }
};
function updateaddress(sId) {
    var _jsParam = new jsonRow();
    _jsParam.AddCell("ACTION", "updateaddress");
    _jsParam.AddCell("SEL_ID", sId||$("#address-list").find("input:radio[name='address']:checked").val()); 
    ajaxComm(_jsParam);
};
function doSaveOrder() {
    if ($("#J_Go").hasClass("btn-go-disabled")) {
        return;
    }
    var $addrtem1 = $("#address-list").find("input:radio[name='address']:checked");
    if ($addrtem1.length <= 0) {
        alert("请选择收货地址！");
        return;
    }
    var $orderlisttem1 = $("#dByOrderList .order");
    if ($orderlisttem1.length <= 0) {
        alert("当前没有选择商品！");
        return;
    }
    $("#txtPlaceId").val($addrtem1.attr("place-id"));
    var loadi = layer.load('正在保存中…');
    try {
        function closeload() { layer.close(loadi); }
        var _jsParam = new jsonRow();
        _jsParam.AddCell("ACTION", "doSaveOrder");
        _jsParam.AddCell("txtGoodsId", $("#txtGoodsId").val());
        _jsParam.AddCell("txtCartId", $("#txtCartId").val());
        _jsParam.AddCell("txtSrcT", $("#txtSrcT").val());
        _jsParam.AddCell("txtPubNId1", $("#txtPubNId1").val());
        _jsParam.AddCell("txtPubNId2", $("#txtPubNId2").val());
        _jsParam.AddCell("txtBuyQty", $("#txtBuyQty").val());
        _jsParam.AddCell("txtPlaceId", $("#txtPlaceId").val());
        _jsParam.AddCell("txtPaidScoreQty", $("#txtPaidScoreQty").val());
        _jsParam.AddCell("RECE_ADDRESS_ID", $addrtem1.val());
//        var _jsPost = new jsonRow();
//        _jsPost.AddCell("RECE_ADDRESS_ID", $addrtem1.val());
//        _jsPost.AddCell("ORDER_COUNT", $orderlisttem1.length);
        $orderlisttem1.each(function (i, _item) {
            var $self = $(this);
            _jsParam.AddCell("BUY_MEMO_" + $self.attr("shop-id"), $(this).find(' textarea').val()); 
        });

        ajaxComm(_jsParam, null, closeload);
    }
    catch (err) {
        layer.close(loadi);
    }
};
function updateorderaddr() {
    var $tem321 = $("#address-list .selected");
    if ($tem321.length > 0) {
        $("#sendtozone").html($tem321.attr("dr-full"));
        $("#sendtoreceipt").html($tem321.attr("dr-receipt"));
    }
};
function ShowAddress(sId) {
    if (!$("#J_NewAddressBtn").hasClass("disabled")) {
        //    $.ploadFile("layer", function (script, textStatus) {
        $.layer({
            type: 2,
            shade: [0.5, '#000'],
            fix: true,
            title: '-',
            maxmin: false,
            iframe: { src: '/c_CAddriFrame.htm?ID=' + sId },
            area: ['910px', '430px'],
            end: function (index) { }
        });
        //    });
    }
};